package servlet;

import impl.UserImpl;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import model.User;
import utils.Util;

public abstract class BaseServlet extends HttpServlet {

    private UserImpl userDao;

    @Override
    public void init(ServletConfig config) throws ServletException {
        userDao = UserImpl.getInstance();
        super.init(config);
    }

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        try {
            beforeExecute(request, response);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            throw new ServletException(e);
        }
    }

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        try {
            beforeExecute(request, response);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            throw new ServletException(e);
        }
    }

    /**
     * Здесь выполняются действия общие для всех сервлетов наследуемых от этого.
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    private void beforeExecute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //double time = System.currentTimeMillis();
        /*
        int fixuser_id = Util.fixInt(request.getParameter("fixuser_id"));
        HttpSession session = request.getSession();
        if (session.isNew()) {
            if (fixuser_id != -1) {
                User user = new User();
                user = userDao.getById(fixuser_id);
                if (user != null) {
                    session.setAttribute("user", user);
                } else {
                    request.getRequestDispatcher("/WEB-INF/jsp/error.jsp?error=1").forward(request, response);
                    return;
                }
            } else {
                request.getRequestDispatcher("/WEB-INF/jsp/error.jsp?error=fixuser_id").forward(request, response);
                return;
            }
        }*/
        //request.setAttribute("time", System.currentTimeMillis() - time);
        execute(request, response);
    }

    /**
     * Если будет принят POST или GET запрос этот метод будет вызван.<br>
     * Используется когда при POST или GET запросе нужно выполнять одинаковые
     * действия.
     *
     * @param request -
     * @param response -
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    protected abstract void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException;
}
